package com.lufan.util;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * FFmpeg检查工具类
 */
@Component
public class FFmpegChecker {
    
    @Value("${ffmpeg.path:ffmpeg}")
    private String ffmpegPath;
    
    @PostConstruct
    public void checkFFmpeg() {
        if (ffmpegPath == null || ffmpegPath.trim().isEmpty()) {
            return;
        }
        File ffmpegFile = new File(ffmpegPath);
        if (!ffmpegFile.exists()) {
            return;
        }
        if (!ffmpegFile.canExecute()) {
            return;
        }
        try {
            List<String> command = new ArrayList<>();
            command.add(ffmpegPath);
            command.add("-version");
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder output = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
        } catch (Exception e) {
        }
    }
    
    public boolean isFFmpegAvailable() {
        if (ffmpegPath == null || ffmpegPath.trim().isEmpty()) {
            return false;
        }
        
        File ffmpegFile = new File(ffmpegPath);
        return ffmpegFile.exists() && ffmpegFile.canExecute();
    }
} 